package 多线程.消费者生产者;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Print {

    private volatile int i = 0;

    public synchronized void printA() {
        while (i < 100) {
            try {
                while(i%2!=0){
                    wait();
                }
                System.out.println(Thread.currentThread().getName() + ":" + i);
                i++;
                notifyAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public synchronized void printB() {
        while (i < 100) {
            try {
                while(i%2!=1){
                    wait();
                }
                System.out.println(Thread.currentThread().getName() + ":" + i);
                i++;
                notifyAll();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        Print printer = new Print();
        new Thread(() ->{
            printer.printB();
        },"奇数").start();
        new Thread(() ->{
            printer.printA();
        },"偶数").start();


    }
}


